package com.cyd.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.cyd.homefamily.R;

public class FragmentTab extends View {
	private Bitmap mDefalutBitmap;
	private String mText;
	private int mTextSize;
	private int mColor;

	private Rect mBitmapRect;
	private Rect mTextRect;

	private Paint mBitmapPaint;
	private Paint mTextPaint;

	private float mAlpha = 0.0f;

	private Canvas mCanvas;

	private Bitmap mBitmap;

	public FragmentTab(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.tab);
		int count = a.getIndexCount();
		for (int i = 0; i < count; i++) {
			int attr = a.getIndex(i);
			switch (attr) {
			case R.styleable.tab_defaultIcon:
				BitmapDrawable defDrawable = (BitmapDrawable) a
						.getDrawable(attr);
				mDefalutBitmap = defDrawable.getBitmap();
				break;
			case R.styleable.tab_textSize:
				mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue
						.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12,
								getResources().getDisplayMetrics()));
				break;
			case R.styleable.tab_text:
				mText = a.getString(attr);
				break;
			case R.styleable.tab_color:
				mColor = a.getColor(attr, Color.BLUE);
				break;
			}
		}
		a.recycle();
		mTextPaint = new Paint();
		mTextRect = new Rect();
		mTextPaint.setTextSize(mTextSize);
		mTextPaint.setColor(mColor);
		mTextPaint.getTextBounds(mText, 0, mText.length(), mTextRect);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int bitmapWidth = Math.min(getMeasuredWidth() - getPaddingLeft()
				- getPaddingRight(), getMeasuredHeight() - getPaddingTop()
				- getPaddingBottom() - mTextRect.height());
		int left = getMeasuredWidth() / 2 - bitmapWidth / 2;
		int top = (getMeasuredHeight() - mTextRect.height()) / 2 - bitmapWidth
				/ 2;
		mBitmapRect = new Rect(left, top, left + bitmapWidth, top + bitmapWidth);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		int alpha = (int) Math.ceil(255 * mAlpha);
		canvas.drawBitmap(mDefalutBitmap, null, mBitmapRect, null);
		drawTarBitmap(alpha);
		drawDefaultText(canvas, alpha);
		drawTarText(canvas, alpha);
		canvas.drawBitmap(mBitmap, 0, 0, null);
	}

	private void drawTarBitmap(int alpha) {
		mBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
				Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		mBitmapPaint = new Paint();
		mBitmapPaint.setDither(true);
		mBitmapPaint.setAntiAlias(true);
		mBitmapPaint.setColor(mColor);
		mBitmapPaint.setAlpha(alpha);
		mCanvas.drawRect(mBitmapRect, mBitmapPaint);
		mBitmapPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		mBitmapPaint.setAlpha(255);
		mCanvas.drawBitmap(mDefalutBitmap, null, mBitmapRect, mBitmapPaint);
	}

	private void drawDefaultText(Canvas canvas, int alpha) {
		mTextPaint.setTextSize(mTextSize);
		mTextPaint.setColor(Color.BLACK);
		mTextPaint.setAlpha(255 - alpha);
		canvas.drawText(mText, mBitmapRect.left + mBitmapRect.width() / 2
				- mTextRect.width() / 2,
				mBitmapRect.bottom + 8 + mTextRect.height(), mTextPaint);
	}

	private void drawTarText(Canvas canvas, int alpha) {
		mTextPaint.setColor(mColor);
		mTextPaint.setAlpha(alpha);
		canvas.drawText(mText, mBitmapRect.left + mBitmapRect.width() / 2
				- mTextRect.width() / 2,
				mBitmapRect.bottom + 8 + mTextRect.height(), mTextPaint);
	}

	public void setIconAlpha(float alpha) {
		this.mAlpha = alpha;
		postInvalidate();
	}
}
